﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cureos.Measures;

namespace SensorDrone.Net
{
    /// <summary>
    /// A generic sensor implementation
    /// </summary>
    public class BaseSensor : ISensor
    {
        public string Name { get; set; }
        public string Unit { get; set; }
        public bool IsSi
        {
            get;set;
        }
        public Func<IDroneChannel, bool> Enable { get; set; }
        public Func<IDroneChannel, bool> Disable { get; set; }
        public Func<IDroneChannel, string> Measure { get; set; }
        public Func<IDroneChannel, bool> Status { get; set; }

        
        /// <summary>
        /// Create a basic reading 
        /// </summary>
        /// <param name="reading"></param>
        /// <returns></returns>
        public virtual Reading CreateReading(string reading)
        {
            return new Reading(Name,Unit,reading);
        }
    }
    /// <summary>
    /// A Standard measure (SI) sensor
    /// </summary>
    /// <typeparam name="TQ">The SI quantity eg. Volts, Pressure, Temperature</typeparam>
    public class Sensor<TQ> : BaseSensor where TQ : struct, IQuantity<TQ>
    {
        private readonly IUnit<TQ> _unit;
        private readonly IEnumerable<IUnit<TQ>> _alternates;
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="unit">The unit of measure</param>
        /// <param name="alternates">Alternate units of measure</param>
        public Sensor(IUnit<TQ> unit, IEnumerable<IUnit<TQ>> alternates)
        {
            _unit = unit;
            _alternates = alternates;
            IsSi = true;
        }
        /// <summary>
        /// Create a Standard Measure
        /// </summary>
        /// <param name="value">The sensor sample, as a string</param>
        /// <returns>A Reading</returns>
        public override Reading CreateReading(string value)
        {
            var reading =  new Reading(Name, Unit, value);
            double d = double.Parse(value);
            reading.Measure = new Measure<TQ>(d, _unit);
            reading.AlternateUnits = _alternates;
            return reading;
        }
    }
}
